package com.ruoyi.zsyh.controller.applet.after.factory;

import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.EmptyUtils;
import com.ruoyi.common.utils.IdWorker;
import com.ruoyi.zsyh.domain.*;
import com.ruoyi.zsyh.service.*;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 服务订单 -- 厂家售后配件管理
 */
@Controller
@RequestMapping("/applet/zsyh/afterParts")
public class ZsyhAfterPartsAppletController extends BaseController
{

    @Autowired
    private IZsyhAfterPartsService zsyhAfterPartsService;

    @Autowired
    private IZsyhServiceOrderService zsyhServiceOrderService;


    /**
     * 维修师傅 订单中心 申请配件时候获取配件一级分类
     * @return
     */
    @RequestMapping("/getPartsNameList")
    @ResponseBody
    public AjaxResult getPartsNameList(String orderId)throws Exception
    {

        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);
        String factoryId = order.getFactoryId();
        List<Map> partsNameList = zsyhAfterPartsService.getPartsNameList(factoryId);

        return success("成功", partsNameList);
    }

    /**
     * 维修师傅 订单中心 申请配件时候获取配件二级分类
     * @return
     */
    @RequestMapping("/getPartsAttributeList")
    @ResponseBody
    public AjaxResult getPartsAttributeList(String orderId, String partsName)throws Exception
    {

        ZsyhServiceOrder order = zsyhServiceOrderService.selectZsyhServiceOrderById(orderId);
        String factoryId = order.getFactoryId();

        ZsyhAfterParts parts = new ZsyhAfterParts();
        parts.setFactoryId(factoryId);
        parts.setPartsName(partsName);

        List<ZsyhAfterParts> partsNameList = zsyhAfterPartsService.selectZsyhAfterPartsList(parts);

        return success("成功", partsNameList);
    }

    @RequestMapping("/getPartsListByFactoryId")
    @ResponseBody
    public AjaxResult getPartsListByFactoryId(String factoryId, String partsName)throws Exception
    {


        ZsyhAfterParts parts = new ZsyhAfterParts();
        parts.setFactoryId(factoryId);
        parts.setPartsName(partsName);

        List<ZsyhAfterParts> partsNameList = zsyhAfterPartsService.selectZsyhAfterPartsList(parts);

        return success("成功", partsNameList);
    }

    /**
     * 厂家 获取配件列表
     * @return
     */
    @RequestMapping("/getPartsListsByFactoryId")
    @ResponseBody
    public AjaxResult getPartsListsByFactoryId(String pageNum, String pageSize, String factoryId, String typeId, String partsId,
                                                String partsName, String partsAttribute, String partsDelState)throws Exception
    {
        ZsyhAfterParts parts = new ZsyhAfterParts();
        parts.setFactoryId(factoryId);
        parts.setPartsId(partsId);
        parts.setPartsName(partsName);
        parts.setPartsAttribute(partsAttribute);
        parts.setPartsDelState(partsDelState);

        List<ZsyhAfterParts> listSize = zsyhAfterPartsService.selectZsyhAfterPartsList(parts);
        int total = listSize.size();
        Map map = new HashedMap();
        if(EmptyUtils.isNotEmpty(pageNum) && EmptyUtils.isNotEmpty(pageSize)){
            int num = Integer.valueOf(pageNum);
            int size = Integer.valueOf(pageSize);
            num = (num - 1) * size;
            map.put("beginPos", num);
            map.put("pageSize", size);
        }
        parts.setParams(map);
        List<ZsyhAfterParts> list = zsyhAfterPartsService.selectZsyhAfterPartsList(parts);
        return success("成功", list, total);
    }

    /**
     * 新增配件
     * @return
     */
    @RequestMapping("/saveParts")
    @ResponseBody
    public AjaxResult saveParts(String factoryId, String partsId,
     String partsName, String partsAttribute, String partsImages, String partsRemarks)throws Exception
    {
        ZsyhAfterParts parts = new ZsyhAfterParts();
        parts.setId(IdWorker.getId());
        parts.setFactoryId(factoryId);
        parts.setPartsId(partsId);
        parts.setPartsName(partsName);
        parts.setPartsAttribute(partsAttribute);
        parts.setPartsImages(partsImages);
        parts.setPartsRemarks(partsRemarks);
        zsyhAfterPartsService.insertZsyhAfterParts(parts);
        return success("成功");
    }


    /**
     * 修改配件
     * @return
     */
    @RequestMapping("/updateParts")
    @ResponseBody
    public AjaxResult updateParts(String id, String partsId,
                                String partsName, String partsAttribute, String partsImages, String partsRemarks)throws Exception
    {
        ZsyhAfterParts parts = new ZsyhAfterParts();
        parts.setId(id);
        parts.setPartsId(partsId);
        parts.setPartsName(partsName);
        parts.setPartsAttribute(partsAttribute);
        parts.setPartsImages(partsImages);
        parts.setPartsRemarks(partsRemarks);
        zsyhAfterPartsService.updateZsyhAfterParts(parts);
        return success("成功");
    }

    /**
     * 配件移入移出回收站
     * @param id
     * @param partsDelState
     * @return
     * @throws Exception
     */
    @RequestMapping("/updatePartsState")
    @ResponseBody
    public AjaxResult updatePartsState(String id, String partsDelState)throws Exception
    {
        ZsyhAfterParts parts = new ZsyhAfterParts();
        parts.setId(id);
        parts.setPartsDelState(partsDelState);
        zsyhAfterPartsService.updateZsyhAfterParts(parts);
        return success("成功");
    }

    /**
     * 回收站商品配件删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/delParts")
    @ResponseBody
    public AjaxResult delParts(String id)throws Exception
    {
        zsyhAfterPartsService.deleteZsyhAfterPartsById(id);
        return success("成功");
    }

}
